package com.ru.application.creditscore.bean;

import java.io.Serializable;
import java.util.ArrayList;  
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;  
import javax.faces.event.ActionEvent;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.model.SelectItem;
import com.ru.application.creditscore.domain.Car;
import org.primefaces.model.LazyDataModel;  
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import com.ru.application.creditsource.model.CarDataModel;

public class DataTableBean implements Serializable {
    private LazyDataModel<Car> lazyModel;  
    private static final long serialVersionUID = 1L;
    private final static String[] colors;  
    private final static String[] manufacturers;  
    
    static {  
        colors = new String[10];  
        colors[0] = "Black";  
        colors[1] = "White";  
        colors[2] = "Green";  
        colors[3] = "Red";  
        colors[4] = "Blue";  
        colors[5] = "Orange";  
        colors[6] = "Silver";  
        colors[7] = "Yellow";  
        colors[8] = "Brown";  
        colors[9] = "Maroon";  
          
        manufacturers = new String[10];  
        manufacturers[0] = "Mercedes";  
        manufacturers[1] = "BMW";  
        manufacturers[2] = "Volvo";  
        manufacturers[3] = "Audi";  
        manufacturers[4] = "Renault";  
        manufacturers[5] = "Opel";  
        manufacturers[6] = "Volkswagen";  
        manufacturers[7] = "Chrysler";  
        manufacturers[8] = "Ferrari";  
        manufacturers[9] = "Ford";  
    }  
  
    private List<Car> cars;  
    private Car selectedCar;  
    private SelectItem[] manufacturerOptions;  
    private List<Car> filteredCars;  
    private CarDataModel mediumCarsModel;
    
    public DataTableBean() {  
	cars = new ArrayList<Car>();  
        populateRandomCars(cars, 50);
        manufacturerOptions = createFilterOptions(manufacturers);
        //mediumCarsModel = new CarDataModel(cars);  if datatable does not rowKey attribute, we must have to use SelectableDataModel
        mediumCarsModel = new CarDataModel(cars);
    }  
      
    public Car getSelectedCar() {  
        return selectedCar;  
    }  
  
    public void setSelectedCar(Car selectedCar) {  
        this.selectedCar = selectedCar;  
    }  
  
    private void populateRandomCars(List<Car> list, int size) {  
        for(int i = 0 ; i < size ; i++)  
            list.add(new Car(getRandomModel(new Integer(i)), getRandomYear(), getRandomManufacturer(), getRandomColor(),false));  
    }  
  
    public List<Car> getCars() {  
        return cars;  
    }  
  
    public void setCars(List<Car> cars) {  
        this.cars = cars;  
    } 
    
    private int getRandomYear() {  
        return (int) (Math.random() * 50 + 1960);  
    }  
      
    private String getRandomColor() {  
        return colors[(int) (Math.random() * 10)];  
    }  
      
    private String getRandomManufacturer() {  
        return manufacturers[(int) (Math.random() * 10)];  
    }  
      
    private Integer getRandomModel(Integer model) {  
        return model;  
    }  
    
    public List<Car> getFilteredCars() {  
        return filteredCars;  
    }  
    
    @SuppressWarnings("unchecked")
    public List<Car> getFilteredCarsWithSelectableDataModel() {  
        return (List<Car>) mediumCarsModel.getWrappedData();  
    } 
    
    public void setFilteredCarsWithSelectableDataModel(List<Car> pMediumCarsModel) {  
        mediumCarsModel.setWrappedData(pMediumCarsModel);  
    } 
    
    public void setFilteredCars(List<Car> filteredCars) {  
        this.filteredCars = filteredCars;  
    }  
    
    public void removeCar(ActionEvent actionEvent){
	String commandId = actionEvent.getComponent().getId();
	System.out.println("commandId=" + commandId);
	if(commandId.compareToIgnoreCase("confirm") == 0) {
	    if(selectedCar != null) {
		System.out.println("getSelectedCar()=" + selectedCar.getName());
		cars.remove(selectedCar);
		System.out.println("new size()=" + cars.size());
	    } else {
		System.out.println("selectedCar is null");
	    }
	} 
    }
    
    public void changeCar() {  
	System.out.println("changeCar");
	Car car = (Car)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car"); 
	System.out.println(car.getName());
	System.out.println(car.getYear());
	this.setSelectedCar(car);
    } 
    
    public void deleteSelectedCar() {  
   	System.out.println("deleteSelectedCar");
   	Car car = (Car)FacesContext.getCurrentInstance().getExternalContext().getRequestMap().get("car"); 
    } 
    
    private SelectItem[] createFilterOptions(String[] data)  {  
        SelectItem[] options = new SelectItem[data.length + 1];  
        options[0] = new SelectItem("", "Select");
        for(int i = 0; i < data.length; i++) {  
            options[i + 1] = new SelectItem(data[i], data[i]);  
        }  
        return options;  
    }  
    
    public SelectItem[] getManufacturerOptions() {  
        return manufacturerOptions;  
    }  
    
    public CarDataModel getMediumCarsModel() {  
        return mediumCarsModel;  
    }  
  
    public void onRowSelect(SelectEvent event) {  
        FacesMessage msg = new FacesMessage("Car Selected", ((Car) event.getObject()).getName().toString() + " -- " + ((Car) event.getObject()).getManufactor());  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
  
    public void onRowUnselect(UnselectEvent event) {  
        FacesMessage msg = new FacesMessage("Car Unselected", ((Car) event.getObject()).getName().toString() + " -- " + ((Car) event.getObject()).getManufactor());  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }  
}
